package com.answer.petsale.service.impl;

// START: 新增导入
import com.answer.petsale.model.dto.animal.AnimalQueryRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
// END: 新增导入
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.answer.petsale.common.ErrorCode;
import com.answer.petsale.exception.BusinessException;
import com.answer.petsale.model.entity.Animal;
import com.answer.petsale.model.vo.AnimalVO;
import com.answer.petsale.service.AnimalService;
import com.answer.petsale.mapper.AnimalMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class AnimalServiceImpl extends ServiceImpl<AnimalMapper, Animal> implements AnimalService {

    /**
     * 对宠物信息进行校验
     *
     * @param animal 宠物对象
     * @param add    是否为新增操作
     */
    @Override
    public void validAnimal(Animal animal, boolean add) {
        if (animal == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = animal.getName();
        String breed = animal.getBreed();
        BigDecimal price = animal.getPrice();

        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(name, breed) || price == null) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "宠物名称、品种和价格不能为空");
            }
        }
        // 对价格进行校验
        if (price != null && price.compareTo(BigDecimal.ZERO) < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "价格不能为负数");
        }
        // 对名称长度进行校验
        if (StringUtils.isNotBlank(name) && name.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
    }

    // START: 新增方法实现
    /**
     * 根据查询请求构建查询条件
     * @param animalQueryRequest 查询请求 DTO
     * @return QueryWrapper
     */
    @Override
    public QueryWrapper<Animal> getQueryWrapper(AnimalQueryRequest animalQueryRequest) {
        if (animalQueryRequest == null) {
            return new QueryWrapper<>();
        }
        QueryWrapper<Animal> queryWrapper = new QueryWrapper<>();

        String species = animalQueryRequest.getSpecies();
        Integer gender = animalQueryRequest.getGender();
        Integer saleStatus = animalQueryRequest.getSaleStatus();
        String searchText = animalQueryRequest.getName(); // 假设 name 字段用作模糊搜索

        // 拼接查询条件
        queryWrapper.like(StringUtils.isNotBlank(searchText), "name", searchText);
        queryWrapper.eq(StringUtils.isNotBlank(species), "species", species);
        queryWrapper.eq(gender != null, "gender", gender);
        queryWrapper.eq(saleStatus != null, "sale_status", saleStatus); // 使用数据库真实列名 sale_status

        // 默认按创建时间降序排序
        queryWrapper.orderByDesc("createTime");

        return queryWrapper;
    }
    // END: 新增方法实现

    /**
     * 将 Animal 实体转换为 AnimalVO 视图
     *
     * @param animal 宠物实体
     * @return AnimalVO
     */
    @Override
    public AnimalVO getAnimalVO(Animal animal) {
        if (animal == null) {
            return null;
        }
        AnimalVO animalVO = new AnimalVO();
        BeanUtils.copyProperties(animal, animalVO);
        return animalVO;
    }

    /**
     * 将分页的 Animal 实体列表转换为 AnimalVO 视图列表
     *
     * @param animalPage 宠物实体分页对象
     * @return 分页的 AnimalVO
     */
    @Override
    public Page<AnimalVO> getAnimalVOPage(Page<Animal> animalPage) {
        List<Animal> animalList = animalPage.getRecords();
        Page<AnimalVO> animalVOPage = new Page<>(animalPage.getCurrent(), animalPage.getSize(), animalPage.getTotal());
        if (CollectionUtils.isEmpty(animalList)) {
            return animalVOPage;
        }
        // 填充VO数据
        List<AnimalVO> animalVOList = animalList.stream().map(this::getAnimalVO).collect(Collectors.toList());
        animalVOPage.setRecords(animalVOList);
        return animalVOPage;
    }
}